﻿
namespace storge
{
	using System;
	using System.Collections.Generic;
	using System.IO;

	public class PhysicalFolder : IFolder
	{
		private DirectoryInfo Folder { get; set; }

		public PhysicalFolder(string path)
		{
			this.Folder = new DirectoryInfo(path);
		}

		public string Path
		{
			get
			{
				return this.Folder.FullName;
			}
		}

		public bool Exists
		{
			get { return Directory.Exists(this.Path); }
		}

		public IEnumerable<IFolder> Subfolders
		{
			get
			{
				List<IFolder> folders = new List<IFolder>();

				foreach (DirectoryInfo subDir in this.Folder.GetDirectories())
				{
					folders.Add(new PhysicalFolder(subDir.FullName));
				}

				return folders;
			}
		}

		public IEnumerable<IFile> Files
		{
			get
			{
				List<IFile> files = new List<IFile>();

				if (this.Exists)
				{
					foreach (FileInfo file in this.Folder.GetFiles())
					{
						files.Add(new PhysicalFile(file.FullName));
					}
				}

				return files;
			}
		}

		public IFolder Subfolder(string subfolderName)
		{
			if (string.IsNullOrEmpty(subfolderName))
				throw new ArgumentNullException("subfolderName", "Subfolder cannot be blank");

			return new PhysicalFolder(System.IO.Path.Combine(this.Path, subfolderName));
		}
	}
}
